package cn.iocoder.yudao.module.obj.service.base;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.obj.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.obj.dal.dataobject.base.ObjBaseInfoDO;
import cn.iocoder.yudao.module.obj.dal.dataobject.base.ObjDocInfoDO;
import cn.iocoder.yudao.module.obj.dal.dataobject.base.ObjDrmInfoDO;
import cn.iocoder.yudao.module.obj.dal.dataobject.base.ObjPlanInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.obj.dal.mysql.base.ObjBaseInfoMapper;
import cn.iocoder.yudao.module.obj.dal.mysql.base.ObjDocInfoMapper;
import cn.iocoder.yudao.module.obj.dal.mysql.base.ObjDrmInfoMapper;
import cn.iocoder.yudao.module.obj.dal.mysql.base.ObjPlanInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.obj.enums.ErrorCodeConstants.*;

/**
 * 项目基础信息表  Service 实现类
 *
 * @author 测试用户
 */
@Service
@Validated
public class ObjBaseInfoServiceImpl implements ObjBaseInfoService {

    @Resource
    private ObjBaseInfoMapper objBaseInfoMapper;
    @Resource
    private ObjDocInfoMapper docInfoMapper;
    @Resource
    private ObjDrmInfoMapper drmInfoMapper;
    @Resource
    private ObjPlanInfoMapper objPlanInfoMapper;

    @Override
    public Long createBaseInfo(ObjBaseInfoSaveReqVO createReqVO) {
        // 插入
        ObjBaseInfoDO baseInfo = BeanUtils.toBean(createReqVO, ObjBaseInfoDO.class);
        objBaseInfoMapper.insert(baseInfo);
        // 返回
        return baseInfo.getId();
    }

    @Override
    public void updateBaseInfo(ObjBaseInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseInfoExists(updateReqVO.getId());
        // 更新
        ObjBaseInfoDO updateObj = BeanUtils.toBean(updateReqVO, ObjBaseInfoDO.class);
        objBaseInfoMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBaseInfo(Long id) {
        // 校验存在
        validateBaseInfoExists(id);
        // 删除
        objBaseInfoMapper.deleteById(id);

        // 删除子表
        deleteDocInfoByXmid(id);
        deleteDrmInfoByXmid(id);
        deletePlanInfoByXmid(id);
    }

    private void validateBaseInfoExists(Long id) {
        if (objBaseInfoMapper.selectById(id) == null) {
            throw exception(BASE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public ObjBaseInfoDO getBaseInfo(Long id) {
        return objBaseInfoMapper.selectById(id);
    }

    @Override
    public PageResult<ObjBaseInfoDO> getBaseInfoPage(ObjBaseInfoPageReqVO pageReqVO) {
        return objBaseInfoMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（项目文档信息表 ） ====================

    @Override
    public PageResult<ObjDocInfoDO> getDocInfoPage(PageParam pageReqVO, String xmid) {
        return docInfoMapper.selectPage(pageReqVO, xmid);
    }

    @Override
    public Long createDocInfo(ObjDocInfoDO docInfo) {
        docInfoMapper.insert(docInfo);
        return docInfo.getId();
    }

    @Override
    public void updateDocInfo(ObjDocInfoDO docInfo) {
        // 校验存在
        validateDocInfoExists(docInfo.getId());
        // 更新
        docInfoMapper.updateById(docInfo);
    }

    @Override
    public void deleteDocInfo(Long id) {
        // 校验存在
        validateDocInfoExists(id);
        // 删除
        docInfoMapper.deleteById(id);
    }

    @Override
    public ObjDocInfoDO getDocInfo(Long id) {
        return docInfoMapper.selectById(id);
    }

    private void validateDocInfoExists(Long id) {
        if (docInfoMapper.selectById(id) == null) {
            throw exception(DOC_INFO_NOT_EXISTS);
        }
    }

    private void deleteDocInfoByXmid(Long xmid) {
        docInfoMapper.deleteByXmid(xmid);
    }

    // ==================== 子表（项目资源设备信息表 ） ====================

    @Override
    public PageResult<ObjDrmInfoDO> getDrmInfoPage(PageParam pageReqVO, String xmid) {
        return drmInfoMapper.selectPage(pageReqVO, xmid);
    }

    @Override
    public Long createDrmInfo(ObjDrmInfoDO drmInfo) {
        drmInfoMapper.insert(drmInfo);
        return drmInfo.getId();
    }

    @Override
    public void updateDrmInfo(ObjDrmInfoDO drmInfo) {
        // 校验存在
        validateDrmInfoExists(drmInfo.getId());
        // 更新
        drmInfoMapper.updateById(drmInfo);
    }

    @Override
    public void deleteDrmInfo(Long id) {
        // 校验存在
        validateDrmInfoExists(id);
        // 删除
        drmInfoMapper.deleteById(id);
    }

    @Override
    public ObjDrmInfoDO getDrmInfo(Long id) {
        return drmInfoMapper.selectById(id);
    }

    private void validateDrmInfoExists(Long id) {
        if (drmInfoMapper.selectById(id) == null) {
            throw exception(DRM_INFO_NOT_EXISTS);
        }
    }

    private void deleteDrmInfoByXmid(Long xmid) {
        drmInfoMapper.deleteByXmid(xmid);
    }

    // ==================== 子表（项目实施计划信息表 ） ====================

    @Override
    public PageResult<ObjPlanInfoDO> getPlanInfoPage(PageParam pageReqVO, String xmid) {
        return objPlanInfoMapper.selectPage(pageReqVO, xmid);
    }

    @Override
    public Long createPlanInfo(ObjPlanInfoDO planInfo) {
        objPlanInfoMapper.insert(planInfo);
        return planInfo.getId();
    }

    @Override
    public void updatePlanInfo(ObjPlanInfoDO planInfo) {
        // 校验存在
        validatePlanInfoExists(planInfo.getId());
        // 更新
        objPlanInfoMapper.updateById(planInfo);
    }

    @Override
    public void deletePlanInfo(Long id) {
        // 校验存在
        validatePlanInfoExists(id);
        // 删除
        objPlanInfoMapper.deleteById(id);
    }

    @Override
    public ObjPlanInfoDO getPlanInfo(Long id) {
        return objPlanInfoMapper.selectById(id);
    }

    private void validatePlanInfoExists(Long id) {
        if (objPlanInfoMapper.selectById(id) == null) {
            throw exception(PLAN_INFO_NOT_EXISTS);
        }
    }

    private void deletePlanInfoByXmid(Long xmid) {
        objPlanInfoMapper.deleteByXmid(xmid);
    }

}